package com.iplus.ydkf.ehome.module.launch;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.iplus.ydkf.ehome.App;
import com.iplus.ydkf.ehome.R;
import com.iplus.ydkf.ehome.module.MainActivity;
import com.iplus.ydkf.ehome.module.base.BaseActivity;
import com.iplus.ydkf.ehome.module.login.LoginActivity;

import butterknife.Bind;

/**
 * Created by liuhaitian on 16/4/26.
 */
public class LaunchActivity extends BaseActivity {
    private long launchTime = 0;
    @Bind(R.id.sdv_launch)
    ImageView sdvLaunch;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (App.getCurrentUser() == null) {
                startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
            } else {
                startActivity(new Intent(LaunchActivity.this, MainActivity.class));
            }
            finish();
        }
    };

    @Override
    public int bindLayout() {
        return R.layout.activity_launch;
    }

    @Override
    public void initView(View view) {
        launchTime = System.currentTimeMillis();
        sdvLaunch.setBackgroundResource(R.drawable.welcome);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setStutasBar();
        super.onCreate(savedInstanceState);
    }

    private void setStutasBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    @Override
    public void initData(View view) {
        final App app = (App) getApplication();
        app.getLocation();
        new Thread() {
            @Override
            public void run() {
                super.run();
                while (!app.isLocationed() || System.currentTimeMillis() - launchTime < 3000) {
                    try {
                        Thread.sleep(1000);
                        if (System.currentTimeMillis() - launchTime >= 7000) {
                            break;
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                handler.sendEmptyMessage(0);
            }
        }.start();

    }

    @Override
    public String getFragmentName() {
        return null;
    }


}
